<template>
	<view class="u-p-l-30 u-p-r-30">
		<u--form labelPosition="left" :model="formData" ref="form" :rules = 'rules'>
			<u-form-item label="昵称:" prop="name" borderBottom required>
				<u--input v-model="formData.name" border="none" placeholder="请输昵称"></u--input>
			</u-form-item>
		</u--form>
		<button @tap="submit" :style="[inputStyle]" class="getCaptcha">修改</button>
	</view>
</template>

<script>
	let _this;
	export default {
		data() {
			return {
				formData: {
					name: ''
				},
				rules:{
					name:[// 必填规则
						{
							type: 'string',
							required: true,
							message: '昵称必填',
							trigger: ['blur']
						},
						// 正则判断为字母、数字、汉字
						{
							pattern: /^[0-9a-zA-Z_\u4e00-\u9fa5]*$/g,
							// 正则检验前先将值转为字符串
							transform(value) {
								return String(value);
							},
							message: '昵称只能包含字母、数字、汉字',
							trigger: ['blur', 'change']
						},
						// 6-8个字符之间的判断
						{
							min: 3,
							max: 8,
							message: '昵称长度在3-8个字符之间',
							trigger: ['blur', 'change']
						}]
				}
			}
		},
		computed: {
			inputStyle() {
				let style = {};
				if (this.formData.name) {
					style.color = "#fff";
					style.backgroundColor = uni.$u.color['warning'];
				}
				return style;
			}
		},
		onLoad() {
			if (!uni.$u.utils.isLogin()) {
				return;
			}
			_this = this;
			const userinfo = uni.getStorageSync('userinfo');
			this.formData.name = userinfo.name;
		},
		methods: {
			submit(){
				this.$refs.form.validate().then(res => {
					//校验通过，res为真
					_this.update();
				}).catch(err => {
					console.log('表单错误信息：', err);
				})
				
			},
			async update(){
				const access_token = uni.getStorageSync('access_token');
				let res = await uni.$u.http.put('/api/user',
					 {name:_this.formData.name},{header:{Authorization:'bearer '+access_token}}
				);
				if(res !== false){ //如果参数错误，会返回422，正确，会返回204。拦截器中，422会返回false
					//更新成功后，获取个人信息，重新缓存
					let res = await uni.$u.http.get('/api/user',{header:{Authorization:'bearer ' + access_token}});						
					uni.setStorage({
						key:'userinfo',
						data:res
					})	
					uni.$u.toast('更新成功');
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.input {
		width: 100%;
		font-size: 16px;
		color: #666;
		border: 1px #e5e5e5 solid;
		border-radius: 5px;
		box-sizing: border-box;
		padding: 0 10px;
		height: 35px;
	}

	.getCaptcha {
		margin: 40rpx 0;
	}

	.u-border-bottom,
	.hint {
		margin-bottom: 40rpx !important;
	}

	.wrap {
		font-size: 28rpx;

		.content {
			width: 600rpx;
			margin: 80rpx auto 0;

			.title {
				text-align: left;
				font-size: 60rpx;
				font-weight: 500;
				margin-bottom: 100rpx;
			}

			input {
				text-align: left;
				margin-bottom: 10rpx;
				padding-bottom: 6rpx;
			}

			.tips {
				color: $u-type-info;
				margin-bottom: 60rpx;
				margin-top: 8rpx;
			}

			.getCaptcha {
				background-color: rgb(253, 243, 208);
				color: $u-tips-color;
				border: none;
				font-size: 30rpx;
				padding: 12rpx 0;

				&::after {
					border: none;
				}
			}

			.alternative {
				color: $u-tips-color;
				display: flex;
				justify-content: space-between;
				margin-top: 30rpx;
			}
		}

		.buttom {
			.loginType {
				display: flex;
				padding: 350rpx 150rpx 150rpx 150rpx;
				justify-content: space-between;

				.item {
					display: flex;
					flex-direction: column;
					align-items: center;
					color: $u-content-color;
					font-size: 28rpx;
				}
			}

			.hint {
				padding: 20rpx 40rpx;
				font-size: 20rpx;
				color: $u-tips-color;

				.link {
					color: $u-type-warning;
				}
			}
		}
	}
</style>
